import { _decorator, animation, Animation, Component, EventMouse, Node } from 'cc'
import { MouseManager } from './MouseManager'
const { ccclass, property } = _decorator

@ccclass('Cell')
export class Cell extends Component {
  public currentPlant: Node = null

  protected onLoad(): void {
    this.node.on(Node.EventType.MOUSE_DOWN, this.onMouseDown, this)
    this.node.on(Node.EventType.MOUSE_MOVE, this.onMouseMover, this)
  }

  protected onDestroy(): void {
    this.node.off(Node.EventType.MOUSE_DOWN, this.onMouseDown, this)
    this.node.off(Node.EventType.MOUSE_MOVE, this.onMouseMover, this)
  }

  onMouseDown(event: EventMouse) {
    MouseManager.Instance.onCellClick(this)
  }

  onMouseMover(event: EventMouse) {
    MouseManager.Instance.onMousMove(event)
  }

  addPlant(plant: Node): boolean {
    if (this.currentPlant == null) {
      this.currentPlant = plant
      this.currentPlant.setPosition(this.node.getPosition())
      // 播放动画
      this.currentPlant.getComponent(animation.AnimationController).enabled = true
      return true
    } else {
      return false
    }
  }
}
